package com.aaa.cars.service.impl;

import com.aaa.cars.dao.BrandDao;
import com.aaa.cars.entity.Brand;
import com.aaa.cars.entity.BrandBigpic;
import com.aaa.cars.service.BrandBigpicService;
import com.aaa.cars.service.BrandService;
import com.aaa.common.constants.ExceptionConstant;
import com.aaa.common.constants.ReturnStatus;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 车辆品牌表(Brand)表服务实现类
 *
 * @author makejava
 * @since 2020-12-04 16:54:29
 */
@Service("brandService")
@Transactional
public class BrandServiceImpl implements BrandService {
    @Resource
    private BrandDao brandDao;

    @Resource
    private BrandBigpicService brandBigpicService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Brand queryById(Long id) {
        return this.brandDao.queryById(id);
    }

    /**
     * 通过实体作为筛选条件查询
     *
     * @param pageNo
     * @param pageSize
     * @param brand    实例对象
     * @return 对象列表
     */
    @Override
    public Result queryAll(int pageNo, int pageSize, Brand brand) {
        if(pageNo==0||pageSize==0){
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(),
                    ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }
        //分页
        PageHelper.startPage(pageNo,pageSize);
        PageInfo<Brand> pageInfo = new PageInfo<>(brandDao.queryAll(brand));
        return new Result(ReturnStatus.SUCCESS.getReturnCode(),
                ReturnStatus.SUCCESS.getReturnMsg(), pageInfo);
    }

    /**
     * 查询全部car下拉框使用
     * @param brand 实例对象
     * @return
     */
    @Override
    public Result queryAlls(Brand brand) {
        return new Result(ReturnStatus.SUCCESS.getReturnCode(),
                ReturnStatus.SUCCESS.getReturnMsg(),brandDao.queryAll(brand));
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Brand> queryAllByLimit(int offset, int limit) {
        return this.brandDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param brand 实例对象
     * @return 实例对象
     */
    @Override
    public Brand insert(Brand brand) {
        this.brandDao.insert(brand);
        return brand;
    }

    /**
     * 修改数据
     *
     * @param brand 实例对象
     * @return 实例对象
     */
    @Override
    public Brand update(Brand brand) {
        this.brandDao.update(brand);
        return this.queryById(brand.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.brandDao.deleteById(id) > 0;
    }
}